<?php echo $header; ?>
<div class="box">
    <div class="content">
        <h2 class="h2title">Search Document</h2>
        <div>
            <div class="warning" style="margin-top:10px;display:none;">
            </div>
            <table style="width:100%;padding:20px;">
                <tr>
                    <td align="right">
                        <span class="spanlablebold" style="margin-right:5px;">Document No</span>
                    </td>
                    <td>
                        <input type="text" class="txt" id="txtdocno" name="txtdocno" maxlength="50"/>
                    </td>
                    <td align="right">
                        <span class="spanlablebold" style="margin-right:5px;">Document Type</span>
                    </td>
                    <td>
                        <!-- <input type="text" class="txt" id="txtdoctypename" name="txtdoctypename" maxlength="50"/>-->
                        <select id="seldoctype" class="sel" style="width: 205px;">
                            <option value=""></option>
                            <?php if(count($doctype_list)>0){
						foreach($doctype_list as $row){ ?>
                            <option value="<?php echo $row["docuementtype_id"]; ?>">
                                <?php echo $row["name"]; ?>
                            </option>
                            <?php }}?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <span class="spanlablebold" style="margin-right:5px;">Reference No</span>
                    </td>
                    <td>
                        <input type="text" class="txt" id="txtrefrenceno" name="txtrefrenceno" maxlength="50"/>
                    </td>
                    <td align="right">
                        <span class="spanlablebold" style="margin-right:5px;">Received from</span>
                    </td>
                    <td>
                        <input type="text" class="txt" id="txtreceivedfrom" name="txtreceivedfrom" maxlength="50"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <span class="spanlablebold" style="margin-right:5px;">Received on</span>
                    </td>
                    <td>
                        <input type="text" class="txt" id="txtreceivedon" name="txtreceivedon" maxlength="50"/>
                    </td>
                    <td align="right">
                        <span class="spanlablebold" style="margin-right:5px;">Addressed to</span>
                    </td>
                    <td>
                        <input type="text" class="txt" id="txtaddressto" name="txtaddressto" maxlength="50"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        <span class="spanlablebold" style="margin-right:5px;">Document date</span>
                    </td>
                    <td>
                        <input type="text" class="txt" id="txtdocdate" name="txtdocdate" maxlength="50"/>
                    </td>
                    <td colspan="2">
                    </td>
                </tr>
                <tr>
                    <td align="center" colspan="4">
                        <input type="button" value="Search" id="btnsearch" name="btnsave" class="btnradius" style="margin-right:10px;"/>
                        <?php if($this->user->getAccessLevel()!=3){ ?>
                        <input type="button" value="History" id="btnhistory" name="btnhistory" class="btnradius" style="margin-right:10px;"/>
                        <?php }?>
                        <input type="button" value="Reset" id="btnreset" name="btnreset" class="btnradius"/>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</div>
<div class="box" style="margin-top:10px;">
    <div class="content">
        <table id="tblsearch" class="list">
            <thead>
                <td class="left" style="width:15%">
                    Document No
                </td>
                <td class="left" style="width:15%">
                    Department Code
                </td>
                <td class="left" style="width:15%">
                    Department Name
                </td>
                <td class="left" style="width:20%">
                    Receiving Date
                </td>
                <td class="left" style="width:20%">
                    Receiving Time
                </td>
            </thead>
            <?php if(count($search_list)>0){
			foreach($search_list as $row){ 
			?>
            <tbody>
                <tr>
                    <td class="left">
                        <?php echo $row["document_no"]; ?>
                    </td>
                    <td>
                        <?php echo $row["departmentcode"]; ?>
                    </td>
                    <td>
                        <?php echo $row["name"]; ?>
                    </td>
                    <td>
                        <?php echo $row["received_date"]; ?>
                    </td>
                    <td>
                        <?php echo $row["received_time"]; ?>
                    </td>
                </tr>
                <?php }}else { ?>
                <tr>
                    <td colspan="4" class="center">
                        No Record Found!
                    </td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>
<script type="text/javascript">
    
    $("#txtreceivedon,#txtdocdate").datepicker({
        changeMonth: true,
        changeYear: true
    });
	
	$("#btnhistory").click(function(){
		 searchlist("historypost");
		
	});
    
    $("#btnsearch").click(function(){
      searchlist("searchpost");
    });
	
	function searchlist(arg){
		var surl= "<?php echo MAIN_PAGE.'search/';?>"+arg;
		  loading("show");
        $.ajax({
            url:surl,
            type: 'POST',
            data: {
                docno: $("#txtdocno").val(),
                doctype: $("#seldoctype").val(),
                refno: $("#txtrefrenceno").val(),
                receivedfrom: $("#txtreceivedfrom").val(),
                receivedon: $("#txtreceivedon").val(),
                addressto: $("#txtaddressto").val(),
                docdate: $("#txtdocdate").val()
            },
            dataType: 'json',
            success: function(res){
                loading('hide');
                
                if (res.status) {
                    $("#tblsearch tbody tr").remove();
                    if (res.searchcount > 0) {
                        var resarr = res.searchresult;
                        // alert(resarr.length);
                        for (var i = 0; i < resarr.length; i++) {
                            var smsg = "<tr>" +
                            "<td class='left'>" +
                            resarr[i].document_no +
                            "</td>" +
                            "<td>" +
                            resarr[i].departmentcode +
                            "</td>" +
                            "<td>" +
                            resarr[i].name +
                            "</td>" +
                            "<td>" +
                            resarr[i].received_date +
                            "</td>" +
                            "<td>" +
                            resarr[i].received_time +
                            "</td>" +
                            "</tr>";
                            $("#tblsearch tbody").append(smsg);
                        }
                        
                    }
                    else {
                        $("#tblsearch tbody").append("<tr><td class='center' colspan='5'>No Record Found</td></tr>");
                    }
                }
                else {
                    alert(res.msg);
                }
            }
        });
	}
    
    $("#btnreset").click(function(){
        var txtarr = ["txtdocno", "txtrefrenceno", "txtreceivedfrom", "txtreceivedon", "txtaddressto", "txtdocdate"];
        for (var i = 0; i < txtarr.length; i++) {
            $("#" + txtarr[i]).val("");
        }
        $("#seldoctype option:first").attr('selected', 'selected');
    });
	
</script>
<?php echo $footer;?>
